package com.whg.demo.model;

import lombok.Data;

import java.io.Serializable;

@Data
public class JsonData implements Serializable {

    private static final long serialVersionID = 1L;

    private Integer code; //状态码：0-成功，1-处理中，-1-失败
    private Object data; //数据
    private String msg; //描述

    public JsonData(){
    }

    public JsonData(Integer code, Object data, String msg){
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    //成功，无返回数据
    public static JsonData buildSuccess(){
        return new JsonData(0,null,"操作成功");
    }

    //成功，有返回数据
    public static JsonData buildSuccess(Object data){
        return new JsonData(0,data,"操作成功");
    }

    //失败，无描述信息
    public static JsonData buildError(){
        return new JsonData(-1,null,"操作失败");
    }

    //失败，传入描述信息
    public static JsonData buildError(String msg){
        return new JsonData(-1,null,msg);
    }

    //失败，传入描述信息，状态码
    public static JsonData buildError(String msg,Integer code){
        return new JsonData(code,null,msg);
    }

    public static JsonData buildError(Object data,String msg){
        return new JsonData(-1,data,msg);
    }

    //成功，传入数据及描述信息
    public static JsonData buildSuccess(Object data,String msg){
        return new JsonData(1,data,msg);
    }

    //成功，传入数据及状态码
    public static JsonData buildSuccess(Object data,Integer code){
        return new JsonData(code,data,"操作成功");
    }
}
